<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product_Types_Entity extends CI_Model
{
    private $tablename = 'product_types';
    private $id = null;
    private $title = null;
    private $alias = null;
    private $status = null;
    private $created = null;
    private $changed = null;
    private $sid = null;
    
    public function setId($Id)
    {
        $this->id = $Id;
    }

    public function setTitle($Title)
    {
        $this->title = $Title;
    }

    public function setAlias($Alias)
    {
        $this->alias = $Alias;
    }

    public function setStatus($Status)
    {
        $this->status = $Status;
    }

    public function setCreated($Created)
    {
        $this->created = $Created;
    }

    public function setChanged($Changed)
    {
        $this->changed = $Changed;
    }

    public function setSid($Sid)
    {
        $this->sid = $Sid;
    }

    public function checkExist_stores($sid)
    {
        $query = $this->db->get_where('stores', array('id' => $sid));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function checkExistFK_products($id)
    {
        $query = $this->db->get_where('products', array('tid' => $id));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function save()
    {
        $params = array();
        foreach ($this as $key => $val) {
            if ($key == 'tablename') continue;
            if ($val == null || $val == '') continue;
            $params[$key] = $val;
        }
        
        // foreign key check
        if (!isset($params['sid']) || !$this->checkExist_stores($params['sid'])) {
            return -1;
        }        
        if (!isset($params['id'])) {
            if ($this->db->insert($this->tablename, $params)) {
                return $this->db->insert_id();
            } else {
                return 0;
            }
        } else {
            return $this->db->update($this->tablename, $params, array('id' => $params['id']));
        }
        
    }
    
    public function del($id)
    {
        // check exist foreign key
        if ($this->checkExistFK_products($id)) {
            return array('-1', 'product_types'.' '.t('is exist in').' '.'products');
        }
        
        return $this->db->delete($this->tablename, array('id' => $id));    
    }
    
    public function getProduct_TypesById($id)
    {
        return $this->db->get_where($this->tablename, array('id' => $id));
    }
    
    public function getAllList()
    {
        return $this->db->get($this->tablename);
    }
    
    public function TotalProduct_Types()
    {
        return $this->db->count_all_results('Product_Types'); 
    } 
}       
        